package com.lhq.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lhq.business.domain.Chapter;
import com.lhq.business.entity.NovelBook;
import com.lhq.business.mapper.ChapterMapper;
import com.lhq.business.repo.NovelBookRepository;
import com.lhq.business.service.IChapterService;
import com.lhq.common.core.base.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 章节Service业务层处理
 *
 * @author lhq
 * @date 2024-05-11
 */
@Service
public class ChapterServiceImpl  extends BaseServiceImpl<ChapterMapper,Chapter>  implements IChapterService
{
    @Autowired
    private ChapterMapper chapterMapper;
    @Autowired
    private NovelBookRepository novelBookRepository;

    /**
     * 查询章节
     *
     * @param id 章节主键
     * @return 章节
     */
    @Override
    public Chapter selectById(String id)
    {
        return chapterMapper.selectById(id);
    }

    /**
     * 查询章节分页列表
     * @return 章节
     */
    @Override
    public IPage<Chapter> selectList(Page<Chapter> page,Chapter chapter)
    {
        QueryWrapper<Chapter> queryWrapper =new QueryWrapper<>();
        return chapterMapper.selectPage(page,queryWrapper);
    }

    /**
     * 新增章节
     *
     * @param chapter 章节
     * @return 结果
     */
    @Transactional
    @Override
    public int insertChapter(Chapter chapter)
    {
        return chapterMapper.insert(chapter);
    }

    /**
     * 修改章节
     *
     * @param chapter 章节
     * @return 结果
     */
    @Transactional
    @Override
    public int updateChapter(Chapter chapter)
    {
        return chapterMapper.updateById(chapter);
    }

    /**
     * 删除章节信息
     *
     * @param id 章节主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteById(String id)
    {
        Chapter  chapter =new Chapter();
        chapter.setId(id);
        return chapterMapper.update(chapter,Wrappers
                .lambdaUpdate(Chapter.class)
                .eq(Chapter::getId, id)
                .set(Chapter::getDelFlag, "1"));
    }

    @Override
    public int selectCountByBookId(String id) {
        return baseMapper.selectCountByBookId(id);
    }
    /**
     * 获取章节信息
     * @author lhq
     * @date 2024/7/30 14:18
     * @version 1.0
     */
    @Override
    public Chapter getChapterInfo(String bookId, String id) {
        QueryWrapper<Chapter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id)
                .eq("book_id",bookId);
        Chapter chapter = baseMapper.selectOne(queryWrapper);
        if(chapter != null){
            NovelBook novelBook = new NovelBook();
            novelBook.setChapterId(Integer.parseInt(id));
            novelBook.setBookId(bookId);
            List<NovelBook> all = novelBookRepository.findAll(novelBook);
            if(all.size() > 0){
                chapter.setContent(all.get(0).getContent());
            }
        }
        return chapter;
    }
}
